package com.smartwebx.ha.web.gis.module.screen;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.assign.HaStationAreaManager;
import com.smartwebx.ha.biz.assign.HaStationGroupCfgManager;
import com.smartwebx.ha.biz.assign.HaStationStationGroupManager;
import com.smartwebx.ha.biz.rainwater.real.StationManager;
import com.smartwebx.ha.dal.assign.bean.HaStationGroupCfg;
import com.smartwebx.ha.dal.usebean.ShowStationInfo;
import com.smartwebx.ha.dal.usebean.StationAdminArea;
import com.smartwebx.ha.dal.usebean.StationInfo;
import com.smartwebx.ha.dal.util.MyFunctions;

public class StationScreen extends BasicScreen {
	@Autowired
	private StationManager stationService;
	@Autowired
	private HaStationAreaManager haStationAreaService;
	@Autowired
	private HaStationStationGroupManager haStationStationGroupService;
	@Autowired
	private GisInfoScreen gisInfoScreen;
	@Autowired
	private HaStationGroupCfgManager haStationGroupCfgService;
	@Autowired
	private StationManager stationManager;

	/**
	 * 获得站码的基础信息
	 * 
	 * @param stcd
	 *            (62729200)
	 * @return
	 */
	public StationInfo getStationInfo(String stcd) {
		StationInfo stationInfo = stationService.getStationInfo(stcd);
		return stationInfo;
	}

	/**
	 * 根据 areaId 获得基本信息
	 * 
	 * @param areaId
	 * @return
	 */
	public Collection<ShowStationInfo> getStationInfos() {
		return stationService.getStationInfos(getAreaId());
	}

	public Collection<ShowStationInfo> getStationInfosByStatGrpupId(
			Integer statGroupId) {
		return stationService.getStationInfosByStatGroup(statGroupId);
	}

	/**
	 * 
	 * 获得根据行政区域划分的水雨情站
	 * 
	 * @return
	 */
	public Collection<StationAdminArea> getStationsByAdminArea() {
		return haStationAreaService.getStationsByAdminArea(getAreaId());
	}

	public Collection<StationAdminArea> getAreaStationsByAdminArea(
			Integer statGroupId) {
		return haStationAreaService.getAreaStationsByAdminArea(getAreaId(),
				statGroupId);
	}

	/**
	 * 区域站群操作
	 * 
	 * @param stcds
	 * @return
	 */
	public Object updateAreaStations(String stcds, String opeart) {
		List<String> stcdSplit = Splitter.on(',').omitEmptyStrings()
				.splitToList(stcds);

		if (!stcdSplit.isEmpty()) {
			Integer areaId = getAreaId();
			if (Objects.equal("add", opeart)) {// 添加
				haStationAreaService.addStationArea(areaId, stcdSplit);
			} else if (Objects.equal("rm", opeart)) {// 删除
				haStationAreaService.removeStationArea(areaId, stcdSplit);
			}
		}
		return true;
	}

	/**
	 * 设置区域站群目录
	 * 
	 * @return
	 */
	public Collection<ShowHaStationGroupCfg> getHaStationGroupCfgByAreaId() {
		final List<HaStationGroupCfg> stationGroupCfgs = haStationGroupCfgService
				.getHaStationGroupCfgByAreaId(getAreaId());

		List<ShowHaStationGroupCfg> result = Lists.newArrayList();
		for (HaStationGroupCfg haStationGroup : stationGroupCfgs) {
			ShowHaStationGroupCfg showHsgc = new ShowHaStationGroupCfg();
			BeanUtils.copyProperties(haStationGroup, showHsgc);
			if (Objects.equal(1, haStationGroup.getLevel())) {
				final String parentCode = haStationGroup.getParentCode();
				Optional<HaStationGroupCfg> find = Iterables.tryFind(
						stationGroupCfgs, new Predicate<HaStationGroupCfg>() {
							public boolean apply(HaStationGroupCfg input) {
								return Objects.equal(input.getCode(),
										parentCode);
							}
						});
				if (find.isPresent()) {
					showHsgc.setParentName(find.get().getName());
				}
			}
			result.add(showHsgc);
		}
		return result;
	}

	class ShowHaStationGroupCfg extends HaStationGroupCfg {
		private String parentName;

		public String getParentName() {
			return parentName;
		}

		public void setParentName(String parentName) {
			this.parentName = parentName;
		}
	}

	/**
	 * 增加区域站群目录
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public void addHaStationGroupCfg(String data) throws IOException {
		ObjectMapper mapper = new ObjectMapper();
		HaStationGroupCfg haStationGroupCfg = mapper.readValue(data,
				HaStationGroupCfg.class);

		if (haStationGroupCfg.getId() == null) {
			haStationGroupCfgService.addHaStationGroupCfg(haStationGroupCfg,
					getAreaId());
		} else if (haStationGroupCfg.getId() != null) {
			haStationGroupCfgService.updateHaStationGroupCfg(haStationGroupCfg,
					getAreaId());
		}
	}

	/**
	 * 删除区域站群目录
	 * 
	 * @param id
	 * @return
	 */
	public void rmHaStationGroupCfg(Integer id) {
		haStationGroupCfgService.rmHaStationGroupCfg(id);
	}

	/**
	 * 获得所有的一级站群名
	 * 
	 * @return
	 */
	public List<HaStationGroupCfg> getHaStationGroupCfgFirstLevels() {
		return haStationGroupCfgService
				.getHaStationGroupCfgFirstLevel(getAreaId());
	}

	public HaStationGroupCfg getHaStatoinGroupCfgById(Integer id) {
		return haStationGroupCfgService.getHaStatoinGroupCfgById(id);
	}

	/**
	 * 获得GIS站群标签
	 * 
	 * @return
	 */
	public List<String> getGisStatGroupTab() {
		return haStationGroupCfgService.getGisStatGroupTab(getAreaId());
	}

	/**
	 * 添加、更新 ha_station_stationgroup 表
	 * 
	 * @param statId
	 * @param statGroupId
	 */
	public void updateHaStationStationGroups(String operate, String statId,
			Integer statGroupId) {
		List<String> statLists = Splitter.on(',').splitToList(statId);
		List<Integer> statIds = Lists.transform(statLists,
				MyFunctions.parseNumberFunctionFor(Integer.class));
		haStationStationGroupService.updateStationStationGroup(operate,
				getAreaId(), statIds, statGroupId);
	}

	/**
	 * 根据站群Id获得已经选择的站
	 * 
	 * @param statGroupId
	 * @return
	 */
	public List<Integer> getCheckedStation(Integer statGroupId) {
		List<Integer> ids = haStationStationGroupService
				.getStatIdByStatGroupId(statGroupId);
		return ids;
	}

	/**
	 * 根据站群站群名获得站群下的站点
	 * 
	 * @param statGroupId
	 * @return
	 */
	public String getStatStcd(String statKey) {
		return stationManager.getGisStations(statKey);
	}

	private int getAreaId() {
		return gisInfoScreen.getAreaId();
	}
}
